import util from "../../util";
import tools from 'public/tools/index';

export default class parentCollection {
    constructor(clz,api) {
        this.util=util;
        this.info=[];
        this.infoTree=[];
        this.infoCascader=[];
        this.list=[];
        this.listTree=[];
        this.listCascader=[];
        this.records=[];
        this.recordsTree=[];
        this.recordsCascader=[];
        this.api=api;
        this.clz=clz;
    }
    /**
     * 批量删除
     * */
    async delete(filter){
        let result = await this.api.delete({condition:filter}).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    // async deleteById(id){
    //     let result = await this.api.deleteById(id).then(flag=>{
    //         if(flag){
    //             this.deleteRecords([id])
    //             return true;
    //         }else{
    //             return false;
    //         }
    //     }).catch(err=>{
    //         return false;
    //     });
    //     return result;
    // }
    async deleteByIds(ids){
        let result = await this.api.deleteByIds(ids).then(flag=>{
            if(flag){
                this.deleteRecords(ids)
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async deleteByNodeIds(nodeIds){
        let result = await this.api.deleteByNodeIds(nodeIds).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async deleteByClassNames(classNames){
        let result = await this.api.deleteByClassNames(classNames).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 条件查询，当前版本，不分页
     * */
    async query(filter){
        let result = await this.api.query({condition:filter}).then(data=>{
            if(!!data) {
                this.records = data;
                this.recordsTree = util.getTree(data, 'type', 'className')
                this.recordsCascader = util.getCascader(data, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async queryByIds(ids){
        let result = await this.api.queryByIds(ids).then(data=>{
            if(!!data) {
                this.records = data;
                this.recordsTree = util.getTree(data, 'type', 'className')
                this.recordsCascader = util.getCascader(data, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 条件查询，当前版本，分页
     * */
    async queryByPage(pageNumber, pageSize,filter){
        let result = await this.api.queryByPage(pageNumber, pageSize, {condition:filter}).then(data=>{
            
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'type', 'className')
                this.recordsCascader = util.getCascader(data.records, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            console.log('queryByPage', err);
            return false;
        });
        return result;
    }

    /**
     * 条件查询，当前版本，分页
     * */
    async queryIncludeUnpublishedByPage(pageNumber, pageSize,filter){
        let result = await this.api.queryIncludeUnpublishedByPage(pageNumber, pageSize, {condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'type', 'className')
                this.recordsCascader = util.getCascader(data.records, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /********************************************************************************************
     * 摘要查询
     * ******************************************************************************************/
    /**
     * 摘要查询，当前版本，不分页
     * */
    async listAllInfo(){
        let result = await this.api.queryInfo({}).then(data=>{
            if(!!data) {
                this.info = data;
                this.infoTree = util.getTree(data, 'type', 'className');
                this.infoCascader = util.getCascader(data, 'type', 'className');
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 摘要查询，当前版本，分页
     * */
    async queryInfoAll(){
        let result = await this.api.queryInfoAll().then(data=>{
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'type', 'className')
                this.recordsCascader = util.getCascader(data.records, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 摘要查询，当前版本，分页
     * */
    async queryInfoByPage(pageNumber, pageSize,filter){
        let result = await this.api.queryInfoByPage(pageNumber, pageSize, {condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'type', 'className')
                this.recordsCascader = util.getCascader(data.records, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /********************************************************************************************
     * 版本管理
     * ******************************************************************************************/
    /**
     * 摘要查询，所有版本，不分页
     * */
    async listAllVersionInfo(){
        let result = await this.api.queryInfoIncludeUnpublished({}).then(data=>{
            if(!!data) {
                this.info = data;
                this.infoTree = util.getTree(data, 'type', 'className');
                this.infoCascader = util.getCascader(data, 'type', 'className');
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 摘要查询，当前版本，分页包括未发布的
     * */
    async listAllVersionInfoByPage(pageNumber, pageSize,filter){
        let result = await this.api.queryInfoIncludeUnpublishedByPage(pageNumber, pageSize, {condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'type', 'className')
                this.recordsCascader = util.getCascader(data.records, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 全部查询，所有版本，分页
     * */
    async listAllVersion(){
        let result = await this.api.queryIncludeUnpublished({}).then(data=>{
            if(!!data) {
                this.list = data;
                this.listTree = util.getTree(data, 'type', 'className')
                this.listCascader = util.getCascader(data, 'type', 'className')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    // 公共方法，用来操作this.records
    addRecord(record) {
        this.records.unshift(record);
    }
    updateRecord(record, key='id') {
        let index = this.records.findIndex(item => item[key] === record[key]);
        if (index !== -1) {
          this.records.splice(index, 1, record)
        }
    }
    deleteRecords(list, key='id') {
        list.forEach(value => {
            let index = this.records.findIndex(item => item[key] === value);
            if (index !== -1) {
                this.records.splice(index, 1)
            }
        })
    }
    calcFilter(filter, searchCondition) {
        let newFilter = ''
        if (tools.isReal(filter)) {
          newFilter = util.getFilter(filter);
        }
        newFilter = searchCondition + (searchCondition && newFilter ? ' and ' : '') + newFilter
        return newFilter
    }
}
